package sharedspace;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;


public class FileHandler {
	/**
	 *  excepted extensions
	 */
	public static final String[] extenstions = {"doc", "docx"};
	/**
     * Get the extension of a file.
     * @param f - File
     * @return string as the file extension
     */  
    public static String getFileExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
	/**
	 * open the file dialog chooser for the teacher to select the test  upload file
	 * @return File
	 */
	public static File openFileDialog(String modelName, JTextField txtFileName){
		File file = null;
		String extenstionsStr= modelName + "File (";
		for(String str : extenstions){
			extenstionsStr+="*." +str + ",";
		}
		extenstionsStr = extenstionsStr.substring(0, extenstionsStr.length()-1) + ")";
		JFileChooser fileopen = new JFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter(extenstionsStr, extenstions);
		fileopen.addChoosableFileFilter(filter);
        int retval = fileopen.showDialog(null, "Open file");
        if (retval == JFileChooser.APPROVE_OPTION) {
            //... The user selected a file
            file = fileopen.getSelectedFile();

            //... Update user interface.
            txtFileName.setText(file.getName());
        }// end if
        return file;
	}// end function openFileDialog
	
	public static File openFileDialog(String modelName){
		File file = null;
		String extenstionsStr= modelName + "File (";
		for(String str : extenstions){
			extenstionsStr+="*." +str + ",";
		}
		extenstionsStr = extenstionsStr.substring(0, extenstionsStr.length()-1) + ")";
		JFileChooser fileopen = new JFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter(extenstionsStr, extenstions);
		fileopen.addChoosableFileFilter(filter);
        int retval = fileopen.showDialog(null, "Open file");
        if (retval == JFileChooser.APPROVE_OPTION) {
            //... The user selected a file
            file = fileopen.getSelectedFile();

            //... Update user interface.
           // path = (file.getName());
        }// end if
        return file;
	}// end function openFileDialog
	
	public static File saveFileDialog(String modelName){
		File file = null;
		String extenstionsStr= modelName + "File (";
		for(String str : extenstions){
			extenstionsStr+="*." +str + ",";
		}
		extenstionsStr = extenstionsStr.substring(0, extenstionsStr.length()-1) + ")";
		JFileChooser fileopen = new JFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter(extenstionsStr, extenstions);
		fileopen.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fileopen.addChoosableFileFilter(filter);
        int retval = fileopen.showSaveDialog(null);
        if (retval == JFileChooser.APPROVE_OPTION) {
            //... The user selected a file
            file = fileopen.getSelectedFile();

            //... Update user interface.
            //path = (file.getName());
        }// end if
        return file;
	}// end function openFileDialog
	
	/**
	 * Check if the selected teacher file is a valid.
	 * for the type of:  {doc, docx, pdf, xls, xlsx, txt}
	 * @return true if contain as extension only one of the above extensions
	 */
	public static boolean isValidFileExtension(File file){
		String fileExt=FileHandler.getFileExtension(file);
		for(String ext : extenstions){
			if(fileExt.equalsIgnoreCase(ext))
				return true;
		}// end for
		return false;
	}// end function isValidFileExtension

}
